<?php

/**
* alltosun.com 评价函数 comment_helper.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-9-9 下午5:39:34 $
* $Id$
*/

class comment_helper
{
    public static function get_comment_ratio($goods_id, $star = 5)
    {
        if (!$goods_id) {
            return false;
        }

        $count = _model('comment')->getTotal(array('goods_id'=>$goods_id, 'star'=>5));

        $total = _model('comment')->getTotal(array('goods_id'=>$goods_id));

        $num = ($count / $total) * 100;

        $num = number_format($num, 2, '.', '');

        if (substr($num, -2) == 00) {
            $num = substr($num, 0, -3);
        }

        return $num;
    }

    public static function get_comment_user($comment_id, $status)
    {
        if (!$comment_id) {
            return false;
        }
        if (!$status) {
            return false;
        }

        return _model('comment_user')->getTotal(array('comment_id'=>$comment_id, 'comment_status'=>$status));
    }

    public static function get_count_comment($goods_id, $star = 0) {
        if (!$goods_id) {
            return 0;
        }

        $filter['goods_id'] = $goods_id;
        if ($star) {
            $filter['star'] = $star;
        }

        $count = 0;
        $count = _model('comment')->getTotal($filter);

        return $count;
    }

    public static function get_count_official($comment_id) {
        if (!$comment_id) {
            return 0;
        }

        $count = 0;

        $count = _model('comment')->getTotal(array('parent_id'=>$comment_id));

        return $count;
    }

    public static function get_official_list($comment_id)
    {
        if (!$comment_id) {
            return array();
        }

        $list = array();

        $list = _model('comment')->getList(array('parent_id'=>$comment_id));

        return $list;
    }

    public static function get_comment_list_by_goods_id($goods_id, $num = 10)
    {
        if (!$goods_id) {
            return array();
        }
        $list = array();
        $list = _model('comment')->getList(array('goods_id'=>$goods_id), ' ORDER BY `add_time` DESC LIMIT '.$num);
        return $list;
    }

    public static function get_comment_img($comment_id = 0, $num = 5)
    {
        if (!$comment_id) {
            return array();
        }

        $ids = _model('attachment_relation')->getFields('attachment_id', array('res_name'=>'comment', 'res_id'=>$comment_id));

        if (!$ids) {
            return array();
        }
        $list = _model('attachment')->getList($ids, ' ORDER BY `add_time` DESC LIMIT '.$num);

        return $list;
    }

    public static function get_comment_label($num = '6')
    {
        $list = _model('comment_label')->getList(array('id >'=>0), ' LIMIT '.$num);

        return $list;
    }

    /**
     * 获取单个商品评论总数
     */
    public static function get_comment_num($goods_id, $res_name='goods_snapshot')
    {
        if (!$goods_id) {
            return 0;
        }

        $comment_ids = comment_helper::get_comment_ids($goods_id, $res_name);

        return count($comment_ids);
    }

    /**
     * 通过 goods_id 查找评论ids
     */
    public static function get_comment_ids($goods_id, $res_name='goods_snapshot')
    {
        if (!$goods_id) {
            return array();
        }

        $filter['res_name'] = $res_name;
        $filter['res_id'] = _model('goods_snapshot')->getFields('id', array('goods_id'=>$goods_id));

        return _model('comment')->getFields('id', $filter);
    }

    /**
     * 获取用户是否已经评论了某资源
     */
    public static function check_is_comment($res_id, $res_name='goods_snapshot')
    {
        if (!$res_id) {
            return 0;
        }

        $user_id = user_helper::get_user_id();
        if (!$user_id) {
            return 0;
        }

        $filter = array(
                'res_id'   => $res_id,
                'res_name' => $res_name,
                'user_id'  => $user_id
        );

        $comment_id = _uri('comment', $filter, 'id');

        if ($comment_id) {
            return $comment_id;
        }

        return 0;
    }

    /**
     * 获取是否评论了订单
     */
    public static function check_is_order_relation($res_id, $order_relation_id)
    {
        if (!$res_id) {
            return 0;
        }

        $user_id = user_helper::get_user_id();
        if (!$user_id) {
            return 0;
        }

        $filter = array(
                'res_id'   => $res_id,
                'res_name' => 'goods_snapshot',
                'user_id'  => $user_id,
                'attach'   => 'order_relation'.$order_relation_id
        );

        $comment_id = _uri('comment', $filter, 'id');

        if ($comment_id) {
            return $comment_id;
        }

        return 0;
    }

    /**
     * 获取当前商品总分
     */
    public static function get_comment_star($goods_id)
    {
        if (!$goods_id) {
            return 5;
        }

        $comment_list = _model('comment')->getList(array('goods_id'=>$goods_id, 'status'=>1));
        if (!$comment_list) {
            return 5;
        }
        $comment_num = 0;
        $comment_star = 0;
        foreach ($comment_list as $k=>$v) {
            $comment_star += $v['star'];
            $comment_num++;
        }

        return round($comment_star/$comment_num);
    }
}
?>